home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / H_SLIDER.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  14.9 KB  |  529 lines

  1.  
  2. package sub_arctic.lib;
  3.  
  4. import sub_arctic.lib.sub_arctic_error;
  5. import sub_arctic.output.loaded_image;
  6. import sub_arctic.output.drawable;
  7. import java.awt.Color;
  8. import java.util.Vector;
  9.  
  10. /**
  11.  * This the class that knows how to the display-side work of displaying
  12.  * a horizontal scrollbar. It handles outputting a scrollbar from a 
  13.  * set of state variables. 
  14.  * 
  15.  * @author Scott Hudson
  16.  */
  17. public class h_slider_display extends base_interactor {
  18.  
  19.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  20.  
  21.   /**
  22.    * This variable is a boolean which says if you want a box around the
  23.    * slider or not.
  24.    */
  25.   protected boolean _boxed=true;
  26.  
  27.   /**
  28.    * Return whether or not the user wants a box around the slider or
  29.    * not.
  30.    * @return boolean true if the user wants the slider boxed
  31.    */
  32.   public boolean boxed(){ return _boxed;};
  33.  
  34.   /**
  35.    * Modify whether or not to box the slider.
  36.    * 
  37.    * @param boolean b the new boxed state variable
  38.    */
  39.   public void set_boxed(boolean b) { 
  40.     _boxed=b;
  41.     damage_self();
  42.   }
  43.  
  44.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  45.  
  46.   /* this is here so subclasses can draw the thumb at places other than
  47.    * against the top edge of the interactor. This is a shift in the *Y*
  48.    * direction. */
  49.  
  50.   /**
  51.    * This variable is how much to offset (in y and in pixels) the slider's
  52.    * thumb by when drawing it. 
  53.    */
  54.   protected int _thumb_shift=0;
  55.  
  56.   /**
  57.    * Return how much to shift the thumb by (in y, and in pixels).
  58.    * @return int how much to shift the thumb by (in y, and in pixels).
  59.    */
  60.   public int thumb_shift() { return _thumb_shift;}
  61.  
  62.   /**
  63.    * Change the amount to shift the thumb by (in the y direction)
  64.    * @param int v the amount to shift the thumb by in pixels
  65.    */
  66.   public void set_thumb_shift(int v) { 
  67.     _thumb_shift=v;
  68.     damage_self();
  69.   }
  70.  
  71.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  72.   
  73.   /** Image for the right button */
  74.   protected loaded_image _right_img;
  75.   
  76.   /** 
  77.    * Image for the right button 
  78.    * 
  79.    * @return the right image. 
  80.    */
  81.   public loaded_image right_img() {return _right_img;}
  82.   
  83.   /** 
  84.    * Set the image for the right button 
  85.    * 
  86.    * @param loaded_image img the image to use for the right button.
  87.    */
  88.   public void set_right_img(loaded_image img) {
  89.     _right_img = img;
  90.     damage_self();
  91.   }
  92.  
  93.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  94.  
  95.   /** Image for the left button */
  96.   protected loaded_image _left_img;
  97.  
  98.   /** 
  99.    * Image for the left button 
  100.    * 
  101.    * @return the left image. 
  102.    */
  103.   public loaded_image left_img() {return _left_img;}
  104.  
  105.   /** 
  106.    * Set the image for the left button 
  107.    * 
  108.    * @param loaded_image img the image to use for the left button
  109.    */
  110.   public void set_left_img(loaded_image img) {
  111.     _left_img = img;
  112.     damage_self();
  113.   }
  114.  
  115.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  116.  
  117.   /** Image for the "thumb" */
  118.   protected loaded_image _thumb_img; 
  119.  
  120.   /** 
  121.    * Image for the thumb
  122.    * 
  123.    * @return the thumb image. 
  124.    */
  125.   public loaded_image thumb_img() {return _thumb_img;} 
  126.  
  127.   /** 
  128.    * Set the image for the thumb of the slider
  129.    * 
  130.    * @param loaded_image img the image to use for the slider's thumb
  131.    */
  132.   public void set_thumb_img(loaded_image img) {
  133.     _thumb_img = img;
  134.     damage_self();
  135.   }
  136.  
  137.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  138.  
  139.   /** Image for the background -- use null for a default solid color */
  140.   protected loaded_image _back_img;
  141.  
  142.   /** 
  143.    * Image for the background (pattern). You'll get null for a default 
  144.    * solid color.
  145.    * 
  146.    * @return the background image
  147.    */
  148.   public loaded_image back_img() {return _back_img;}
  149.  
  150.   /** 
  151.    * Set the image for the background -- use null for a default solid color 
  152.    * 
  153.    * @param loaded_image img the image for the background of the slider
  154.    */
  155.   public void set_back_img(loaded_image img) {
  156.     _back_img = img;
  157.     damage_self();
  158.   }
  159.  
  160.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  161.  
  162.   /** Minimum value of the slider */
  163.   protected int _min_val;
  164.  
  165.   /** Minimum value of the slider
  166.    * @return int the minimum value of the slider  */
  167.   public int min_val() {return _min_val;}
  168.  
  169.   /** 
  170.    * Set the minimum value of the slider 
  171.    * 
  172.    * @param int val the new  minimum value of the slider.
  173.    */
  174.   public void set_min_val(int val) {
  175.     _min_val = val;
  176.     damage_self();
  177.   }
  178.  
  179.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  180.  
  181.   /** Maximum value of the slider */
  182.   protected int _max_val;
  183.  
  184.   /** Maximum value of the slider 
  185.   * @return the maximum value of the slider*/
  186.   public int max_val() {return _max_val;}
  187.  
  188.   /** 
  189.    * Set the maximum value of the slider 
  190.    * 
  191.    * @param int val the new maximum value of the slider.
  192.    */
  193.   public void set_max_val(int val) {
  194.     _max_val = val;
  195.     damage_self();
  196.   }
  197.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  198.  
  199.   /** Small increment value */
  200.   protected int _small_inc;
  201.  
  202.   /** 
  203.    * Return the small increment value (this is the amount the slider's 
  204.    * value changes when you push the end buttons).
  205.    * 
  206.    * @return int the small increment value of the slider
  207.    */
  208.   public int small_inc() {return _small_inc;}
  209.  
  210.   /** 
  211.    * Set the small increment value (amount the value changes when you click on 
  212.    * the end buttons.
  213.    * 
  214.    * @param int val the new small increment 
  215.    */
  216.   public void set_small_inc(int val) {_small_inc = val;}
  217.  
  218.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  219.  
  220.   /** Large increment value */
  221.   protected int _large_inc;
  222.  
  223.   /** 
  224.    * Return the large increment value (this is the amount the slider's 
  225.    * value changes when you click in the thumb area but not on the thumb).
  226.    * 
  227.    * @return int the large increment value of the slider
  228.    */
  229.   public int large_inc() {return _large_inc;}
  230.  
  231.   /** 
  232.    * Set the large increment value (amount the value changes when you 
  233.    * click on the thumb area but not on the thumb).
  234.    * 
  235.    * @param int val the new small increment 
  236.    */
  237.   public void set_large_inc(int val) {_large_inc = val;}
  238.  
  239.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  240.  
  241.   /** 
  242.    * Return the value of the part_a component of this object.  In our case 
  243.    * this is stored in _value. 
  244.    * @return int the up to date value of part_a (which is tied to _value).
  245.    */
  246.   public int part_a()
  247.     {
  248.       /* Make sure its up to date and in range then return it */
  249.       eval_part_a();
  250.       conform();
  251.       return _value;
  252.     }
  253.  
  254.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  255.  
  256.   /**
  257.    * Set part_a value directly bypassing the constraint system (but doing 
  258.    * damage).  This should normally only be done by the constraint system 
  259.    * or some other part of the system which takes care of marking things 
  260.    * out-of-date, etc. itself.  In this case part_a is stored in _value.
  261.    *
  262.    * @param int v the new value for part_a (AKA _value).
  263.    */
  264.   protected void set_raw_part_a(int v) 
  265. {
  266.       /* don't do anything unless this is a change */
  267.       if (v != _value)
  268.     {
  269.       /* make change and do damage */
  270.           _value = v;
  271.       conform();
  272.           damage_self();
  273.     }
  274.     }
  275.  
  276.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  277.  
  278.   /** 
  279.    * Set the part_a component of this object.  In our case this is stored in
  280.    * _value.  
  281.    *
  282.    * @param int v the new value for part_a (AKA _value).
  283.    */
  284.   public void set_part_a(int v) 
  285. {
  286.       /* if this has a constraint throw an exception */
  287.       if ((active_constraints() & PART_A) != 0)
  288.         throw new sub_arctic_error(
  289.       "Attempt to assign value to constrained part_a " +
  290.       "(AKA h_slider_display.value)");
  291.  
  292.       /* don't do anything unless this is a change */
  293.       if (v != _value)
  294.     {
  295.       set_raw_part_a(v);
  296.       mark_part_a_ood();
  297.     }
  298.     }
  299.  
  300.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  301.   
  302.   /** Current value of the slider */
  303.   protected int _value;
  304.   
  305.   /** 
  306.    * Current value of the slider 
  307.    * 
  308.    * @return int the current value of this slider
  309.    */
  310.   public int value() {return part_a();}
  311.  
  312.   /** 
  313.    * Set current value of the slider. This value will be forced to be 
  314.    * between the min and the max, inclusive.
  315.    * 
  316.    * @param int val the new value
  317.    */
  318.   public void set_value(int val) 
  319. {
  320.   set_part_a(val);
  321. }
  322.    //had:
  323.    //* @exception general PROPAGATED
  324.  
  325.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  326.  
  327.   /**
  328.    * Full constructor for the h_slider_display.  If you do not supply 
  329.    * an image for any of the image slots (i.e. you pass null) a default
  330.    * look will be applied for you.
  331.    * 
  332.    * @param int          xv       x position of the slider.
  333.    * @param int          yv       y position of the slider.
  334.    * @param int          wv       width of the slider (in pixels).
  335.    * @param int          minv     minimum value of the slider.
  336.    * @param int          maxv     maximum value of the slider.
  337.    * @param int          init_val initial value of the slider.
  338.    * @param int          sincv    small increment value. 
  339.    * @param int          lincv    large increment value.
  340.    * @param loaded_image rt_im    image for the right button.
  341.    * @param loaded_image lf_im    image for the left button.
  342.    * @param loaded_image th_im    image for the thumb.
  343.    * @param loaded_image back_pat image for the background of the slider.
  344.    */
  345.   public h_slider_display(
  346.     int xv, int yv, int wv, 
  347.     int minv, int maxv, 
  348.     int init_val,
  349.     int sincv, int lincv,
  350.     loaded_image rt_im, loaded_image lf_im, loaded_image th_im, 
  351.     loaded_image back_pat)
  352.     {
  353.       /* let the superclass init (with dummy temporary width) */
  354.       super(xv,yv, wv, 15);
  355.  
  356.       /* initialize locals */
  357.       _min_val = minv; _max_val = maxv;
  358.       _value = init_val;
  359.       _small_inc = sincv; _large_inc = lincv;
  360.  
  361.       _right_img = rt_im; _left_img = lf_im; _thumb_img = th_im;
  362.       _back_img = back_pat;
  363.  
  364.       /* use defaults for any nulls */
  365.       if (_right_img == null) _right_img = std.h_slider_right();
  366.       if (_left_img == null)  _left_img  = std.h_slider_left();
  367.       if (_thumb_img == null) _thumb_img = std.h_slider_thumb();
  368.       if (_back_img == null)  _back_img = std.h_slider_back();
  369.  
  370.       /* now reset the height to the real height */
  371.       int max_h = 0; 
  372.       if (max_h < _right_img.height()) max_h = _right_img.height();
  373.       if (max_h < _left_img.height()) max_h = _left_img.height();
  374.       if (max_h < _thumb_img.height()) max_h = _thumb_img.height();
  375.       set_intrinsic_h(max_h);
  376.  
  377.       /* make sure values are all in range and in order */
  378.       conform();
  379.     }
  380.  
  381.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  382.  
  383.   /** 
  384.    * Construct a new h_slider_display with the artkit look.  
  385.    * 
  386.    * @param int xv          x position of the slider.
  387.    * @param int yv          y position of the slider.
  388.    * @param int wv          width of the slider (in pixels).
  389.    * @param int minv        minimum value of the slider.
  390.    * @param int maxv        maximum value of the slider.
  391.    * @param int init_val    initial value of the slider.
  392.    * @param int sincv small increment value.
  393.    * @param int lincv large increment value.
  394.    */
  395.   public h_slider_display(
  396.     int xv, int yv, int wv, 
  397.     int minv, int maxv, 
  398.     int init_val,
  399.     int sincv, int lincv)
  400.     {
  401.       this(xv,yv,wv,minv,maxv,init_val,sincv,lincv, null, null, null, null);
  402.     }
  403.  
  404.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  405.  
  406.   /** 
  407.    * Make sure min <= max and min <= value <= max  by forcing value in range.
  408.    */
  409.   protected void conform()
  410.     {
  411.       int t;
  412.  
  413.       /* make sure min, max are still ok */
  414.       if (_min_val > _max_val)
  415.     {
  416.       t = _min_val; _min_val = _max_val; _max_val = t;
  417.     }
  418.  
  419.       /* force value in range */
  420.       if (_value < _min_val) {_value = _min_val;}
  421.       if (_value > _max_val) {_value = _max_val;}
  422.     }
  423.  
  424.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  425.  
  426.   /** 
  427.    * Indicate that we intrinsically constrain height, but not part_a. 
  428.    * @return int bitset indicating parts we intrinsically constrain. 
  429.    */
  430.   public int intrinsic_constraints()
  431.     {
  432.       return (super.intrinsic_constraints() & ~PART_A) | H;
  433.     }
  434.  
  435.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  436.  
  437.   /** 
  438.    * Compute the offset to the thumb in pixels (returns -1 for no thumb)  
  439.    * 
  440.    * @return int the amount the thumb should be shifted right to account for 
  441.    *             the value of the slider
  442.    */
  443.   protected int thumb_offset()
  444.     {
  445.       int off, slide_range, value_range;
  446.  
  447.       /* compute ranges for thumb */
  448.       slide_range = w() - _right_img.width() - _left_img.width() 
  449.             - _thumb_img.width();
  450.       value_range = max_val() - min_val();
  451.  
  452.       /* return negative if there is no room for the thumb */
  453.       if (slide_range < 0 || value_range < 0) return -1;
  454.       /*
  455.        * Check to keep from getting a divide by zero exception below.  
  456.        * If we just fit, return a zero offset.
  457.        */
  458.       if (slide_range == 0 || value_range == 0) return 0;
  459.  
  460.       /* otherwise compute drawing offset for thumb */
  461.       off = (value() - min_val()) * slide_range / value_range;
  462.       if (off < 0) off = 0;
  463.       if (off > slide_range) off = slide_range;
  464.  
  465.       return off;
  466.     }
  467.  
  468.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  469.  
  470.   /** 
  471.    * Draw the current image on the drawable provided.
  472.    *
  473.    * @param drawable d a drawable for the image or screen you want this 
  474.    *                   object drawn on
  475.    */
  476.   protected void draw_self_local(drawable d) 
  477. {
  478.       int off;
  479.  
  480.       /* fill the rectangle behind the slider */
  481.       if (_back_img != null)
  482.     {
  483.       d.tileImage(_back_img, 0,0,w()-1, h()-1);
  484.     }
  485.  
  486.       /* compute offset for thumb */
  487.       off = thumb_offset();
  488.  
  489.       /* draw the thumb if we have room for one */
  490.       if (off >= 0)
  491.     {
  492.           d.drawImage(_thumb_img, _left_img.width()+off, thumb_shift());
  493.     }
  494.  
  495.       /* draw the buttons */
  496.       d.drawImage(_left_img, 0,0);
  497.       d.drawImage(_right_img, w()-_right_img.width(),0);
  498.  
  499.       /* draw a box around the whole thing if that flag is set 
  500.       * (which it is by default) */
  501.       if (boxed()) {
  502.     d.setColor(Color.black); 
  503.     d.drawRect(0,0, w()-1,h()-1);
  504.       }
  505.     }
  506.  
  507.    //had:
  508.    //* @exception general PROPAGATED
  509.  
  510.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  511.  
  512. }
  513. /*=========================== COPYRIGHT NOTICE ===========================
  514.  
  515. This file is part of the subArctic user interface toolkit.
  516.  
  517. Copyright (c) 1996 Scott Hudson and Ian Smith
  518. All rights reserved.
  519.  
  520. The subArctic system is freely available for most uses under the terms
  521. and conditions described in 
  522.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  523. and appearing in full in the lib/interactor.java source file.
  524.  
  525. The current release and additional information about this software can be 
  526. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  527.  
  528. ========================================================================*/
  529.